<?php
if (!defined('IN_ZWS')) exit();

class GroupAction extends PublicAction {
	function index() {
		$this -> assign('nowNav', 2);
		$this -> assign('topNav', array(array('name' => '组别', 'page' => 'groups'), array('name' => '年级', 'page' => 'grades'), array('name' => '科目', 'page' => 'subjects'), array('name' => '类别', 'page' => 'categories')));
		$this -> display('Public:index');
	}

	function groups() {
		$group = M('GroupGroup');
		$count = $group -> count();
		import('ORG.Util.Page');
		$page = new Page($count, 10);
		$show = $page -> show();
		$groups = $group -> limit($page -> firstRow . ',' . $page -> listRows) -> select();
		$this -> assign('count', $count);
		$this -> assign('page', $show);
		$this -> assign('groups', $groups);
		$this -> display();
	}

	function grades() {
		$grade = D('GradeView');
		$count = $grade -> count();
		import('ORG.Util.Page');
		$page = new Page($count, 10);
		$show = $page -> show();
		$grades = $grade -> limit($page -> firstRow . ',' . $page -> listRows) -> select();
		$this -> assign('count', $count);
		$this -> assign('grades', $grades);
		$this -> assign('page', $show);
		$group = M('GroupGroup');
		$groups = $group -> select();
		$this -> assign('groups', $groups);
		$this -> display();
	}

	function subjects() {
		$subject = M('GroupSubject');
		$count = $subject -> count();
		import('ORG.Util.Page');
		$page = new Page($count, 10);
		$show = $page -> show();
		$subjects = $subject -> limit($page -> firstRow . ',' . $page -> listRows) -> select();
		$this -> assign('count', $count);
		$this -> assign('subjects', $subjects);
		$this -> assign('page', $show);
		$this -> display();
	}

	function categories() {
		$cate = M('GroupCategory');
		$count = $cate -> count();
		import('ORG.Util.Page');
		$page = new Page($count, 10);
		$show = $page -> show();
		$categories = $cate -> limit($page -> firstRow . ',' . $page -> listRows) -> select();
		$this -> assign('count', $count);
		$this -> assign('categories', $categories);
		$this -> assign('page', $show);
		$this -> display();
	}

	function editGroup() {
		$id = intval($this -> _get('id'));
		if (isEmpty($id)) $this -> error('非法操作！');
		$group = M('GroupGroup');
		$groupInfo = $group -> where('id=' . $id) -> find();
		if (!$groupInfo) $this -> error('没有该组别！');
		$this -> assign($groupInfo);
		$this -> display();
	}

	function getGrades() {
		$grade = D('GradeView');
		$gid = intval($this -> _get('gid'));
		$grades = $grade -> where($gid ? ('gid=' . $gid) : '') -> select();
		echo json_encode(array('count' => count($grades), 'grades' => ($grades) ? $grades : ''));
	}

	function getCates() {
		$cate = M('GroupCategory');
		$res = intval($this -> _get('res'));
		$cates = $cate -> where($res != -1 ? ('res=' . $res) : '') -> select();
		echo json_encode(array('count' => count($cates), 'cates' => ($cates) ? $cates : ''));
	}

	function editGrade() {
		$id = intval($this -> _get('id'));
		if (isEmpty($id)) $this -> error('非法操作！');
		$grade = D('GradeView');
		$gradeInfo = $grade -> where('GroupGrade.id=' . $id) -> find();
		if (!$gradeInfo) $this -> error('没有该组别！');
		$group = M('GroupGroup');
		$groups = $group -> select();
		$this -> assign('groups', $groups);
		$this -> assign($gradeInfo);
		$this -> display();
	}

	function editSub() {
		$id = intval($this -> _get('id'));
		if (isEmpty($id)) $this -> error('非法操作！');
		$subject = M('GroupSubject');
		$subjectInfo = $subject -> where('id=' . $id) -> find();
		if (!$subjectInfo) $this -> error('没有该组别！');
		$this -> assign($subjectInfo);
		$this -> display();
	}

	function editCate() {
		$id = intval($this -> _get('id'));
		if (isEmpty($id)) $this -> error('非法操作！');
		$cate = M('GroupCategory');
		$cateInfo = $cate -> where('id=' . $id) -> find();
		if (!$cateInfo) $this -> error('没有该组别！');
		$this -> assign($cateInfo);
		$this -> display();
	}

	function addGroup() {
		switch ($this -> _get('type')) {
			case 1:
				$group = D('AddGroup');
				if ($group -> create ()) {
					if ($group -> add()) {
						$this -> ajaxReturn(array('redirect' => U('groups')), '添加成功，2秒钟后刷新本页面', 0);
					} else {
						$this -> ajaxReturn('', $group -> getError (), 1);
					}
				} else {
					$this -> ajaxReturn('', $group -> getError (), 2);
				}
			case 2:
				$M = M('GroupGrade');
				if ($M -> add(array('gid' => intval($this -> _post('group')), 'cname' => $this -> _post('grade')))) $this -> ajaxReturn(array('redirect' => U('grades')), '添加成功，2秒钟后自动刷新本页面', 0);
				else $this -> ajaxReturn('', '添加失败', 1);
			case 3:
				$subject = D('AddSubject');
				if ($subject -> create ()) {
					if ($subject -> add()) {
						$this -> ajaxReturn(array('redirect' => U('subjects')), '添加成功，2秒钟后自动刷新本页面', 0);
					} else {
						$this -> ajaxReturn('', $subject -> getError (), 1);
					}
				} else $this -> ajaxReturn('', $subject -> getError (), 2);
			case 4:
				$cate = D('AddCategory');
				if ($cate -> create ()) {
					if ($cate -> add()) {
						$this -> ajaxReturn(array('redirect' => U('categories')), '添加成功，2秒钟后自动刷新本页面', 0);
					} else {
						$this -> ajaxReturn('', $cate -> getError (), 1);
					}
				} else {
					$this -> ajaxReturn('', $cate -> getError (), 2);
				}
			default:
				$this -> ajaxReturn('', '非法操作！', 1);
		}
	}

	function doEdit() {
		$id = intval($this -> _get('id'));
		if (isEmpty($id)) $this -> ajaxReturn('', '非法操作！', 1);
		$article = M('Article');
		switch ($this -> _get('type')) {
			case 1:
				$group = M('GroupGroup');
				if ($group -> where('id=' . $id) -> save(array('cname' => $this -> _post('cname'))) !== false) $this -> ajaxReturn(array('redirect' => U('groups')), '编辑成功，2秒钟后回到组别列表', 0);
				else $this -> ajaxReturn('', '编辑失败！', 1);
			case 2:
				$M = M('GroupGrade');
				if ($M -> where('id=' . $id) -> save(array('gid' => intval($this -> _post('group')), 'cname' => $this -> _post('cname'))) !== false) {
					$article -> where(array('gdid' => $id)) -> save(array('gpid' => intval($this -> _post('group'))));
					$this -> ajaxReturn(array('redirect' => U('grades')), '编辑成功，2秒钟后回到年级列表', 0);
				} else $this -> ajaxReturn('', '编辑失败', 1);
				break;
			case 3:
				$M = M('GroupSubject');
				if ($M -> where('id=' . $id) -> save(array('cname' => $this -> _post('cname'))) !== false) $this -> ajaxReturn(array('redirect' => U('subjects')), '编辑成功，2秒钟后回到科目列表', 0);
				else $this -> ajaxReturn('', '编辑失败', 1);
			case 4:
				$cate = M('GroupCategory');
				$res = ($this -> _post('res') > 2 || $this -> _post('res') < 0)? 0 : $this -> _post('res');
				if ($cate -> where('id=' . $id) -> save(array('res' => $res , 'cname' => $this -> _post('cname'))) !== false) {
					$article -> where(array('res=' => $res, 'cid' => $id)) -> save(array('res' => $res));
					$this -> ajaxReturn(array('redirect' => U('categories')), '编辑成功，2秒钟后回到类别列表', 0);
				} else $this -> ajaxReturn('', '编辑失败，请检查名称是否重复！', 1);
			default:
				$this -> ajaxReturn('', '非法操作！', 1);
		}
	}

	function delGroup() {
		$id = intval($this -> _get('id'));
		if (isEmpty($id)) $this -> ajaxReturn('', '非法操作！', 1);
		$article = M('Article');
		switch ($this -> _get('type')) {
			case 1:
				$group = M('GroupGroup');
				if ($group -> delete($id)) {
					$article -> where('gpid=' . $id) -> delete();
					$this -> ajaxReturn('', '删除成功！', 0);
				} else $this -> ajaxReturn('', '删除失败！', 1);
			case 2:
				$grade = M('GroupGrade');
				if ($grade -> delete($id)) {
					$article -> where('gdid=' . $id) -> delete();
					$this -> ajaxReturn('', '删除成功！', 0);
				} else $this -> ajaxReturn('', '删除失败！', 1);
				break;
			case 3:
				$subject = M('GroupSubject');
				if ($subject -> delete($id)) {
					$article -> where('sid=' . $id) -> delete();
					$this -> ajaxReturn('', '删除成功', 0);
				} else $this -> ajaxReturn('', '删除失败', 1);
			case 4:
				$cate = M('GroupCategory');
				if ($cate -> delete($id)) {
					$article -> where('cid=' . $id) -> delete();
					$this -> ajaxReturn('', '删除成功', 0);
				} else $this -> ajaxReturn('', '删除失败', 1);
			default:
				$this -> ajaxReturn('', '非法操作！', 1);
		}
	}
}
